<?php
require_once APPPATH."third_party/wxpayapi/lib/WxPay.Api.php";
require_once APPPATH."third_party/wxpayapi/lib/WxPay.Notify.php";
require_once APPPATH."third_party/wxpayapi/lib/WxPay.JsApiPay.php";
//require_once APPPATH."third_party/wxpayapi/lib/WxPay.NativePay.php";
//require_once APPPATH.'third_party/wxpayapi/lib/phpqrcode.php';
require_once APPPATH."third_party/wxpayapi/lib/log.php";
class Wxpay_model extends Base_Model
{
    private $_log;
    public function __construct()
    {
        parent::__construct();
        $logHandler= new CLogFileHandler(APPPATH."logs/wplogs/".date('Y-m-d').'.log');
        $this->_log = Log::Init($logHandler, 15);
        $this->load->library('wxinterface/Wxpaynotifycallback',NULL,'wxcallback');
    }

    public function addOrder($openId,$uid,$num)
    {
        //①、获取用户openid
        $tools = new JsApiPay();
//        $openId = $tools->GetOpenid();
//        $openId = "oHlcTt7k0U-w5QaTvRqqvDQvfOQQ";
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("能量逗社区-充值");
//        $data = array('num'=>$num,'uid'=>$uid);
//        $datastr = igbinary_serialize($data);
        $input->SetAttach($num.'|'.$uid);
        $randstr = substr(md5(uniqid()),mt_rand(0,15),16);
        $input->SetOut_trade_no(date("YmdHis").$randstr);
        switch ((int)$num){
            case 50:
                $fee = 50;
                break;
            case 100:
                $fee = 100;
                break;
            case 500:
                $fee = 498;
                break;
            case 1000:
                $fee = 995;
                break;
            case 5000:
                $fee = 4950;
                break;
            case 10000:
                $fee = 9850;
                break;
            case 20000:
                $fee = 19650;
                break;
            case 50000:
                $fee = 49000;
                break;
            default:
                return false;
        }
        $input->SetTotal_fee($fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetNotify_url("http://api.nldou.com/wxpay/callback");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }

//    public function nativeOrder($uid,$num)
//    {
//        $notify = new NativePay();
//        $input = new WxPayUnifiedOrder();
//        $input->SetBody("能量逗社区-充值");
//        $input->SetAttach($num.'|'.$uid);
//        $randstr = substr(md5(uniqid()),mt_rand(0,15),16);
//        $input->SetOut_trade_no(date("YmdHis").$randstr);
//        switch ((int)$num){
//            case 50:
//                $fee = 50;
//                break;
//            case 100:
//                $fee = 100;
//                break;
//            case 500:
//                $fee = 498;
//                break;
//            case 1000:
//                $fee = 995;
//                break;
//            case 5000:
//                $fee = 4950;
//                break;
//            case 10000:
//                $fee = 9850;
//                break;
//            case 20000:
//                $fee = 19650;
//                break;
//            case 50000:
//                $fee = 49000;
//                break;
//            default:
//                return false;
//        }
//        $input->SetTotal_fee($fee);
//        $input->SetTime_start(date("YmdHis"));
//        $input->SetTime_expire(date("YmdHis", time() + 600));
//        $input->SetNotify_url("http://api.nldou.com/wxpay/callback");
//        $input->SetTrade_type("NATIVE");
//        $input->SetProduct_id("C201705160001");
//        $result = $notify->GetPayUrl($input);
//        $res['fee']=round($fee/100,2);
//        $res['url']=urlencode($result["code_url"]);
//        $res['num']=$num;
//        return $res;
//    }
//
//    public function qrcode($url)
//    {
//        QRcode::png($url);
//    }

    public function callback()
    {
        $this->wxcallback->Handle(false);
    }
}